package com.jushu.storbox;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.jushu.storbox.actions.AlbumContext;
import com.jushu.storbox.adapter.AlbumCatchSettingAdapter;
import com.jushu.storbox.constant.UiConstant;
import com.jushu.storbox.entities.AlbumEntity;
import com.jushu.storbox.view.dialog.TextProgressDialog;

public class AlbumSelectActivity extends Activity implements OnClickListener, OnItemClickListener {

	private final static int START_REQUEST_ALBUM = 0x01;
	private final static int STOP_REQUEST_ALBUM = 0x02;

	View selectReturn;
	View selectSave;
	GridView albumFolders;

	AlbumContext albumContext = null;

	Intent result = new Intent();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_album_select);
		selectReturn = findViewById(R.id.control_album_select_returRl);
		selectSave = findViewById(R.id.album_setting_save);
		albumFolders = (GridView) findViewById(R.id.album_setting_folders);
		albumFolders.setOnItemClickListener(this);
		selectReturn.setOnClickListener(this);
		selectSave.setOnClickListener(this);
		albumContext = new AlbumContext(this);
		new SetAdapterAsyncTask().execute();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.control_album_select_returRl:
			Log.e("AlbumSelectActivity", "onClick...");
			setResult(UiConstant.SETTING_ALBUM_ACTIVITY_RETURN_CODE, new Intent()); // no
																					// select
			this.finish();
			break;

		case R.id.album_setting_save:
			if (!albumContext.saveAlbumEntity(this)) {
				Toast.makeText(this, R.string.no_sel_folder, Toast.LENGTH_SHORT).show();
				return;
			}
			setResult(UiConstant.SETTING_ALBUM_ACTIVITY_SAVE_CODE, new Intent());// have
																					// select
			this.finish();
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}

	@Override
	public void onBackPressed() {
		Log.e("AlbumSelectActivity", "onBackPressed...");
		setResult(UiConstant.SETTING_ALBUM_ACTIVITY_RETURN_CODE, new Intent());
		super.onBackPressed();
	}

	Handler uiHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {

			case START_REQUEST_ALBUM:
				break;

			case STOP_REQUEST_ALBUM:
				break;

			default:
				super.handleMessage(msg);
			}

		}

	};

	class SetAdapterAsyncTask extends AsyncTask<Integer, Integer, List<AlbumEntity>> {
		TextProgressDialog progressDialog;

		@Override
		protected List<AlbumEntity> doInBackground(Integer... arg0) {
			uiHandler.obtainMessage(START_REQUEST_ALBUM).sendToTarget();
			List<AlbumEntity> list = albumContext.getAlbumList(AlbumSelectActivity.this);
			return list;
		}

		@Override
		protected void onPostExecute(List<AlbumEntity> result) {
			AlbumCatchSettingAdapter adapter = new AlbumCatchSettingAdapter(AlbumSelectActivity.this, result);
			albumFolders.setAdapter(adapter);
			uiHandler.obtainMessage(STOP_REQUEST_ALBUM).sendToTarget();
			progressDialog.dismiss();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			progressDialog = new TextProgressDialog(AlbumSelectActivity.this, R.style.dialog);
			Window wd = progressDialog.getWindow();
			WindowManager.LayoutParams lp = wd.getAttributes();
			lp.alpha = 0.5f;
			wd.setAttributes(lp);
			progressDialog.show();
		}
	}
}
